namespace Resharper.Abstractions.CodeDom.Statements.LocalVariables
{
  using System.Linq;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi;
  using JetBrains.ReSharper.Psi.CSharp.Tree;
  using Resharper.Abstractions.CodeDom.Expressions.ExpressionInitializers;
  using Resharper.Abstractions.CodeDom.Statements.ForStatements;

  /// <summary>Defines the <see cref="ForStatement"/> class.</summary>
  public class LocalVariable
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="ForStatement"/> class.</summary>
    /// <param name="localVariable">The expression.</param>
    internal LocalVariable([NotNull] ILocalVariable localVariable)
    {
      this.InternalLocalVariable = localVariable;
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the initial value.
    /// </summary>
    [CanBeNull]
    public ExpressionInitializer Initializer
    {
      get
      {
        var declaration = this.InternalLocalVariable.GetDeclarations().FirstOrDefault() as ILocalVariableDeclaration;
        if (declaration == null)
        {
          return null;
        }

        var initializer = declaration.Initial as IExpressionInitializer;
        
        return initializer != null ? new ExpressionInitializer(initializer) : null;
      }
    }

    /// <summary>
    /// Gets the name of the type.
    /// </summary>
    [NotNull]
    public string TypeName
    {
      get
      {
        return this.InternalLocalVariable.Type.GetPresentableName(this.InternalLocalVariable.PresentationLanguage);
      }
    }

    /// <summary>
    /// Gets the internal local variable.
    /// </summary>
    [NotNull]
    internal ILocalVariable InternalLocalVariable { get; private set; }

    #endregion
  }
}